package com.tcu.user.controller;

import com.tcu.exception.SmartException;
import com.tcu.result.R;
import com.tcu.user.entity.Permission;
import com.tcu.user.service.IPermissionService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    private IPermissionService permissionService;
    /**
     * 基本操作：
     * 增删改查 ，列表
     */
    @PostMapping("add")
    public R add(@RequestBody Permission permission){
        if(permission.getId()!=null){
            throw new SmartException(20001,"id不为空，无法增加");
        }
        boolean save = permissionService.save(permission);
        return save?R.success():R.error();
    }
    @Delete("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = permissionService.removeById(id);
        return b?R.success():R.error();
    }
    @PostMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<String> ids){
        boolean b = permissionService.removeByIds(ids);
        return b?R.success():R.error();
    }
    @PostMapping("update")
    public R update(@RequestBody Permission permission){
        if(permission.getId()==null){
            throw new SmartException(20001,"id为null，无法更新");
        }
        boolean b = permissionService.updateById(permission);
        return b?R.success():R.error();
    }
    @GetMapping("findById/{id}")
    public R findById(@PathVariable String id){
        Permission byId = permissionService.getById(id);
        return R.success().data("permission", byId);
    }
    @GetMapping("list")
    public R list(){
        List<Permission> list = permissionService.list();
        return R.success().data("list", list);
    }

    @Autowired
    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }
}
